/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
#include <LiquidCrystal.h>

#define ONE_ON 14
#define ONE_OFF 15
#define TWO_ON 16
#define TWO_OFF 17
#define THREE_OFF 18
#define THREE_ON 19
#define BACKLIGHT 13
#define BUTTON 6
#define WAIT 30000
#define BL_OFF 30000
#define OFF_TIME 30000


int timer = 0;
int lastTime = 0;

LiquidCrystal lcd (12, 11, 5, 4, 3, 2);


void setup() {   
  pinMode(BACKLIGHT, OUTPUT);   
  pinMode(ONE_ON, OUTPUT);  
  pinMode(ONE_OFF, OUTPUT); 
  pinMode(TWO_ON, OUTPUT); 
  pinMode(TWO_OFF, OUTPUT); 
  pinMode(BUTTON, INPUT);
  
  digitalWrite(BUTTON, HIGH);
  
  digitalWrite(ONE_ON, LOW);
  digitalWrite(ONE_OFF, LOW);
  digitalWrite(TWO_ON, LOW);
  digitalWrite(TWO_OFF, LOW);
  digitalWrite(THREE_ON, LOW);
  digitalWrite(THREE_OFF, LOW);
  digitalWrite(BACKLIGHT, HIGH);
  
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("  Rebootinator"); 
  
  initOutlets();
  powerUp();
  
  
}

void initOutlets() {
  digitalWrite(ONE_OFF, HIGH);
  delay(500);
  digitalWrite(ONE_OFF, LOW);
  delay(500);
  digitalWrite(TWO_OFF, HIGH);
  delay(500);  
  digitalWrite(TWO_OFF, LOW);
  delay(500);
  digitalWrite(THREE_OFF, HIGH);
  delay(500);
  digitalWrite(THREE_OFF, LOW);  
  delay(500); 
  
} 
  

 
  
void rebootinate() {
  lcd.clear();
  lcd.print(" Rebootinating!");
  initOutlets();
  delay(OFF_TIME);
  powerUp();
  lcd.clear();
  lcd.print("  Rebootinator");
  
}

void powerUp(){  
  digitalWrite(ONE_ON, HIGH);
  delay(500);
  digitalWrite(ONE_ON, LOW);
  lcd.setCursor(0,1);
  lcd.print("One Up...     ");
  delay(WAIT);
  
  
  digitalWrite(TWO_ON, HIGH);
  delay(500);
  digitalWrite(TWO_ON, LOW);
  lcd.setCursor(0,1);
    lcd.print("Two Up...     ");
  delay(WAIT);
  
  digitalWrite(THREE_ON, HIGH);
  delay(500);
  digitalWrite(THREE_ON, LOW);
  lcd.setCursor(0,1);
  lcd.print("All outlets up!");
  delay(5000);
  lcd.clear();
}
  
  
  

void loop() {
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print(millis()/1000);
  
  if(digitalRead(BUTTON) == LOW) {
    delay(20);
      if(digitalRead(BUTTON) == LOW) {
        digitalWrite(BACKLIGHT, HIGH);        
        rebootinate();
        lastTime = millis();
      }
  }
  
  if ((millis() - lastTime) >= BL_OFF) {
   digitalWrite(BACKLIGHT, LOW);
  }
   
   else {
   digitalWrite(BACKLIGHT, HIGH);
 }
 
   


}
